package org.example.day20240827.homeWork;


import java.util.*;

public class Poker {
    private Card[] cards = new Card[52];

    String[] color = {"红桃","草花","梅花","方片"};
    public Poker() {
        int index = 0;
        for(int i=0; i<color.length; i++){
            for (int j=1; j<=13; j++){
                cards[index++] = new Card(color[i],j);
            }
        }
    }

    @Override
    public String toString() {
        String result = "";
        int index = 0;
        for(int i=0; i<4; i++){
            for (int j=1; j<=13; j++){
                result+=cards[index++]+" ";
            }
            result += "\n";
        }

        return result;
    }

    public void shuffle(){
        Random random = new Random();
        for (int i=0; i<52; i++){
            int num = random.nextInt(52);
            Card newCard = cards[num];
            cards[num] = cards[i];
            cards[i] = newCard;
        }
    }

    public List<Card> takeOneHand(){
        List<Card> result = new ArrayList<>();
        for (int i=0; i<5; i++){
            result.add(cards[i]);
        }
        return result;
    }
    public Card getOneCard(int index){
        return cards[index];
    }

    public String confirmType(List<Card> cards){
        List<Integer> cardList = new ArrayList<>();
        Set<Integer> cardSet = new HashSet<>();
        Set<String> cardColerSet = new HashSet<>();
        Map<Integer,Integer> cardMap = new HashMap<>();
        for(Card card : cards){
            cardList.add(card.getNum());
            cardSet.add(card.getNum());
            cardColerSet.add(card.getColor());
            if(cardMap.containsKey(card.getNum())){
                int count = cardMap.get(card.getNum());
                count++;
                cardMap.put(card.getNum(),count);
            }else {
                cardMap.put(card.getNum(),1);
            }
        }

        Collections.sort(cardList);
        boolean IsStraight = false;
        boolean IsSameColor = false;
        if(cardSet.size() == 5 && cardList.get(4)-cardList.get(0) == 4){
            IsStraight = true;
        }
        if(cardColerSet.size() == 1){
            IsSameColor = true;
        }
        if(IsStraight && IsSameColor){
            return "同花顺";
        }
        if(IsStraight){
            return "顺子";
        }
        if(IsSameColor){
            return "同花";
        }
        if(cardSet.size() == 4){
            return "一对";
        }
        if(cardSet.size() == 5){
            return "杂牌";
        }
        if(cardSet.size() == 2){
            if(cardMap.containsValue(4)){
                return "四条";
            }else {
                return "满堂红";
            }
        }
        if(cardSet.size() == 3){
            if(cardMap.containsValue(3)){
                return "三条";
            }else {
                return "两对";
            }
        }

        return "UnKnow";
    }
    public Card[] getCards() {
        return cards;
    }

    public void setCards(Card[] cards) {
        this.cards = cards;
    }

    public String[] getColor() {
        return color;
    }

    public void setColor(String[] color) {
        this.color = color;
    }

    public static void main(String[] args) {
        Poker poker = new Poker();
        poker.shuffle();
        System.out.println(poker);
        System.out.println(poker.takeOneHand());
        System.out.println(poker.confirmType(poker.takeOneHand()));
    }
}
